<?php
	require_once 'Zend/Session/Namespace.php';

	// Session namespace
	$authNamespace = new Zend_Session_Namespace('gcmsadmin');

   	if ($authNamespace->admin != true)
		exit();

	if (isset($_POST['nav'])) {
		$list = explode(',', $_POST['nav']);
		// Check parent links first
		$old = $db->fetchAll("SELECT id,weight FROM menu WHERE parent IS NULL ORDER BY weight");
		$weight = 0;
		foreach ($list as $v) {
			if (substr($v,0,2) == 'nl') {
				if ($old[$weight] != substr($v,2))
					$db->update('menu', array('weight' => $weight), 'id = ' . substr($v,2));
				$weight++;
			}
		}
		// Now do content
		$old = $db->fetchAll("SELECT id,weight FROM menu WHERE parent = 2 ORDER BY weight");
		$weight = 0;
		foreach ($list as $v) {
			if (substr($v,0,2) == 'cl') {
				if ($old[$weight] != substr($v,2))
					$db->update('menu', array('weight' => $weight), 'id = ' . substr($v,2));
				$weight++;
			}
		}
	}
?>
